﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControls_CarbonFootprintQuestion_Waste_RecycleGlassWaste : CarbonExchange.WebControls.AuditQuestionSigleItemBase
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public override void DataBind()
    {
        if(!string.IsNullOrEmpty(Answer.GetSetting<string>("ddl")))
            ddl.Text = Answer.GetSetting<string>("ddl");
        base.DataBind();
    }

    protected override bool SaveQuestion()
    {
        Answer.SetSetting<string>("ddl", ddl.Text);
        CarbonExchange.Bll.AmeeDataItemFactory dataFactory = new CarbonExchange.Bll.AmeeDataItemFactory();
        CarbonExchange.Bll.AmeeDataItem dataItem = null;
        if (!string.IsNullOrEmpty(Answer.SingleItem.AmeeId))
        {
            dataItem = dataFactory.GetByPath("home/waste", Answer.SingleItem.AmeeId);
        }

        if (dataItem == null)
        {
            CarbonExchange.Bll.AmeeDataCatagoryFactory dataCategoryFactory = new CarbonExchange.Bll.AmeeDataCatagoryFactory();
            CarbonExchange.Bll.AmeeDataCatagory dataCategory = dataCategoryFactory.GetByPath("home/waste", "type=glass");
            if (!dataCategory.DataItemSelected)
            {
                throw new ApplicationException(string.Format("Could not find dataitem at '{0}' with choices '{1}'", dataCategory.Path, dataCategory.SelectedAsString()));
            }
            else
            {
                dataItem = dataFactory.GetByPath("home/waste", dataCategory.Id);
            }
        }

        decimal factor = 0m;
        if(ddl.Text == "always")
        {
            factor = 1m;
        }
        else if(ddl.Text == "often")
        {
            factor = 0.75m;
        }
        else if(ddl.Text == "sometimes")
        {
            factor = 0.25m;
        }

        var amountSaved = dataItem.GetValue<decimal>("kgCO2RecyclingSavedPerKg");
        var averageAmount = dataItem.GetValue<decimal>("kgPerPersonPerYear");
        var saving = averageAmount * amountSaved * factor * Audit.PeopleInHousehold / 12m;
        Answer.SetSingleItem(dataItem, saving);
        return true;
    }

}
